package com.bootdo.program.shejiao.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.program.weixin.domain.WxUserDO;
import com.bootdo.program.weixin.service.WxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.shejiao.domain.MemberUserDO;
import com.bootdo.program.shejiao.service.MemberUserService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 会员表
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-10-18 14:20:18
 */
 
@Controller
@RequestMapping("/shejiao/memberUser")
public class MemberUserController extends BaseController {
	@Autowired
	private MemberUserService memberUserService;
	@Autowired
	private WxUserService WxUserService;
	
	@GetMapping()
	@RequiresPermissions("shejiao:memberUser:memberUser")
	String MemberUser(){
	    return "shejiao/memberUser/memberUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shejiao:memberUser:memberUser")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		query.put("appid",queryAppid());
		List<MemberUserDO> memberUserList = memberUserService.list(query);
		memberUserList.forEach(memberUserDO -> {
			WxUserDO wxUserDO = WxUserService.getOpenId(memberUserDO.getUserId());
			WxUserDO wxUserDO2 = WxUserService.getOpenId(memberUserDO.getParentId());
			memberUserDO.setUserName(wxUserDO.getNickName());
		});
		int total = memberUserService.count(query);
		PageUtils pageUtils = new PageUtils(memberUserList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shejiao:memberUser:add")
	String add(){
	    return "shejiao/memberUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shejiao:memberUser:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MemberUserDO memberUser = memberUserService.get(id);
		model.addAttribute("memberUser", memberUser);
	    return "shejiao/memberUser/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shejiao:memberUser:add")
	public R save( MemberUserDO memberUser){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		memberUser.setAppid(getAppid());
		if(memberUserService.save(memberUser)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shejiao:memberUser:edit")
	public R update( MemberUserDO memberUser){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		memberUser.setAppid(getAppid());
		memberUserService.update(memberUser);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shejiao:memberUser:remove")
	public R remove( Long id){
		if(memberUserService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shejiao:memberUser:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		memberUserService.batchRemove(ids);
		return R.ok();
	}
	
}
